// pages/login/login.js
const http = require('../../utils/http.js');

Page({
  data: {
    disabled: true,
    btnstate: "default",
    id_number: "",
    password: "",
  },

  onLoad(data){
    this.setData({
      password: data.password || "",
      id_number: data.id_number || ""
    });
  },

  id_numberInput: function(e) {
    var content = e.detail.value;
    if (content != '') {
      this.setData({
        disabled: false,
        btnstate: "primary",
        id_number: content
      });
    } else {
      this.setData({
        disabled: true,
        btnstate: "default"
      });
    }
  },

  pwdBlur: function(e) {
    var password = e.detail.value;
    this.setData({
      password: password
    });
  },

  login: function() {
    console.log(this.data.id_number);
    console.log(this.data.password);
    const url = `/candidate/login?idNumber=${this.data.id_number}&password=${this.data.password}`;

    http.post(url).then(res => {
      if (res.data.code == 200) {
        console.log("successful");
        const candidate_data = res.data.data;
        const base_candidate_info = {
          id: candidate_data.id,
          id_number: candidate_data.id_number,
          name: candidate_data.name,
          gender: candidate_data.gender,
          birth_date: candidate_data.birth_date,
          ethnicity_id: candidate_data.ethnicity_id,
          political_status_id: candidate_data.political_status_id,
          foreign_language_id: candidate_data.foreign_language_id,
          phone: candidate_data.phone,
          password: candidate_data.password
        };
        const baoming_candidate_info = {
          id_number: candidate_data.id_number,
          vocational_college_id: candidate_data.vocational_college_id,
          vocational_major_id: candidate_data.vocational_major_id,
          candidate_type_id: candidate_data.candidate_type_id,
          exam_center_city_id: candidate_data.exam_center_city_id,
          exam_major_id: candidate_data.exam_major_id,
          volunteer_college_id: candidate_data.volunteer_college_id,
          volunteer_major_id: candidate_data.volunteer_major_id,
          is_certificate_issued: candidate_data.is_certificate_issued
        };
        const post_candidate_info = {
          id_number: candidate_data.id_number,
          address: candidate_data.address,
          postal_code: candidate_data.postal_code,
          recipient_name: candidate_data.recipient_name
        };
        const token = res.data.token;
        const app = getApp();
        app.globalData.token = token;
        console.log(`"this is login page:"+${app.globalData.token} token is over`);
        // 使用同步方法储存
        try {
          wx.setStorageSync('base_candidate_info', base_candidate_info);
          wx.setStorageSync('baoming_candidate_info', baoming_candidate_info);
          wx.setStorageSync('post_candidate_info', post_candidate_info);
          wx.setStorageSync('token', token);
          console.log('更新本地用户信息成功');
          console.log(base_candidate_info);
        } catch(e) {
          console.error('更新本地用户信息失败', e);
        } finally{
          // 跳转页面
          wx.switchTab({
            url: '/pages/mine/mine'
          });
        }
      } else {
        console.log("post请求发送成功，结果出错");
        wx.showToast({
          title: '未知错误',
          icon: 'none',
          duration: 2000
        });
      }
    }).catch(err => {
      console.log('post请求发送失败');
      console.log(err);
      wx.showToast({
        title: '系统错误，请稍后重试',
        icon: 'none',
        duration: 2000
      });
    });
  }
});
